<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="区域名称" prop="regionName">
        <el-input v-model="queryParams.regionName" placeholder="请输入区域名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="区域编码" prop="regionCode">
        <el-input v-model="queryParams.regionCode" placeholder="请输入区域编码" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="regionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="区域ID" align="center" prop="regionId" />
      <el-table-column label="区域名称" align="center" prop="regionName" />
      <el-table-column label="区域编码" align="center" prop="regionCode" />
      <el-table-column label="父级ID" align="center" prop="parentId" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" width="160" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="regionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="区域名称" prop="regionName">
          <el-input v-model="form.regionName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="区域编码" prop="regionCode">
          <el-input v-model="form.regionCode" placeholder="请输入区域编码" />
        </el-form-item>
        <el-form-item label="父级ID" prop="parentId">
          <el-input-number v-model="form.parentId" :min="0" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Region">
import { listRegion, addRegion, updateRegion, delRegion } from '@/api/data/region'

const { proxy } = getCurrentInstance();

const regionList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');
const open = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    regionName: undefined,
    regionCode: undefined,
  },
  rules: {
    regionName: [{ required: true, message: '区域名称不能为空', trigger: 'blur' }],
    regionCode: [{ required: true, message: '区域编码不能为空', trigger: 'blur' }],
  }
});

const { queryParams, form, rules } = toRefs(data);

function getList() {
  loading.value = true;
  listRegion(queryParams.value).then(res => {
    regionList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm('queryRef');
  handleQuery();
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.regionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = '新增区域';
}

function handleUpdate(row) {
  reset();
  form.value = Object.assign({}, row || regionList.value.find(i => i.regionId === ids.value[0]));
  open.value = true;
  title.value = '修改区域';
}

function submitForm() {
  proxy.$refs['regionRef'].validate(valid => {
    if (!valid) return;
    if (form.value.regionId) {
      updateRegion(form.value).then(() => { proxy.$modal.msgSuccess('修改成功'); open.value = false; getList(); });
    } else {
      addRegion(form.value).then(() => { proxy.$modal.msgSuccess('新增成功'); open.value = false; getList(); });
    }
  })
}

function handleDelete(row) {
  const regionIds = row?.regionId || ids.value.join(',');
  proxy.$modal.confirm('是否确认删除区域编号为"' + regionIds + '"的数据项？').then(() => delRegion(regionIds)).then(() => { proxy.$modal.msgSuccess('删除成功'); getList(); }).catch(() => {})
}

function cancel() { open.value = false; reset(); }

function reset() {
  form.value = { regionId: undefined, regionName: undefined, regionCode: undefined, parentId: 0, orderNum: 0, status: '0', remark: '' };
  proxy.resetForm('regionRef');
}

getList();
</script>









